package leetcode;


import java.util.HashSet;
import java.util.Set;

public class Demo03 {
    public static void main(String[] args) {
        String str = "bbbbb";
        System.out.println(lengthOfLongestSubstring(str));
    }

    /**
     * 无重复最长字串
     * 求出每个位置开头时的最长不重复字串
     *
     * @param s
     * @return
     */
    public static int lengthOfLongestSubstring(String s) {
        int max = 0;
        Set<Character> set = new HashSet<>();
        char[] chs = s.toCharArray();
        int i = 0;
        int j = 0;
        while (i < chs.length && j < chs.length) {
            if (!set.contains(chs[j])) {
                set.add(chs[j++]);
                max = Math.max(max, set.size());
            } else {
                set.remove(chs[i++]);
            }
        }

        return max;
    }
}
